package com.android.vgongyi.common;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.android.vgongyi.R;
import com.android.vgongyi.activity.CheckCommentActivity;
import com.android.vgongyi.activity.HomePageActivity;
import com.android.vgongyi.activity.ReplyActivity;
import com.android.vgongyi.activity.SaveImageActivity;
import com.android.vgongyi.activity.TransferActivity;
import com.android.vgongyi.dao.BaseDAO;
import com.android.vgongyi.dao.CommonDAO;
import com.android.vgongyi.dao.FavorateDAO;
import com.android.vgongyi.entity.CommonEntity;
import com.android.vgongyi.utils.DBUtil;

public class WeiboAdapterListener implements OnClickListener{
	private CommonEntity tempCommonEntity;
	private Context context;
	private WeiboAdapter adapter;
	private boolean isOpenWeb = false;
	public WeiboAdapterListener(Context context, WeiboAdapter adapter, CommonEntity tempCommonEntity){
		this.tempCommonEntity = tempCommonEntity;
		this.context = context;
		this.adapter = adapter;
	}
	public void onClick(View v) {
		Intent intent = new Intent();
		switch(v.getId())
		{
		case R.id.add_to_favourate:
			if(tempCommonEntity.getWeibo_id().equals(AppConstant.TAG_AD)){
				return;
			}
			FavorateDAO favorateDAO = new FavorateDAO(context);
			if(DBUtil.checkisExist(BaseDAO.URI_FAVORITE, String.valueOf(tempCommonEntity.getId()), context)){
				favorateDAO.delete("id="+tempCommonEntity.getId(), null);
				tempCommonEntity.setFavourite(false);
				CommonDAO commonDAO = new CommonDAO(context);
				commonDAO.update(tempCommonEntity, "id = '"+tempCommonEntity.getId()+"'", null);
				adapter.notifyDataSetChanged();
				Toast.makeText(context, context.getResources().getString(R.string.favorite_dec), Toast.LENGTH_SHORT).show();
			}else{
				favorateDAO.insert(tempCommonEntity);
				tempCommonEntity.setFavourite(true);
				CommonDAO commonDAO = new CommonDAO(context);
				commonDAO.update(tempCommonEntity, "id = '"+tempCommonEntity.getId()+"'", null);
				CommonToast.show(context);
				adapter.notifyDataSetChanged();
			}
			
			break;
		case R.id.check_comments:
			if(tempCommonEntity.getWeibo_id().equals(AppConstant.TAG_AD)){
				
				if(TextUtils.isEmpty(tempCommonEntity.getAdlink())){
					Toast.makeText(context, "链接地址不可用!", Toast.LENGTH_SHORT).show();
					return ;
				}

				
				if (tempCommonEntity.getBrowser().equals("2")) {
					Uri myBlogUri = Uri.parse(tempCommonEntity.getAdlink());
					Intent urlIntent = new Intent(Intent.ACTION_VIEW, myBlogUri);
					context.startActivity(urlIntent);
				} else if (tempCommonEntity.getBrowser().equals("1")) {
					Intent intentHomePage = new Intent(context,HomePageActivity.class);
					intentHomePage.putExtra("weibo_url",tempCommonEntity.getAdlink());
					context.startActivity(intentHomePage);
				}
			}else{
				if( Integer.valueOf(tempCommonEntity.getComment_nums()) == 0){
					Toast.makeText(context, context.getResources().getString(R.string.reply_no_comments), Toast.LENGTH_SHORT).show();
//					intent.setClass(context, ReplyActivity.class);
//					intent.putExtra("weiboID", tempCommonEntity.getWeibo_id());
//					intent.putExtra("weiboPlatform", tempCommonEntity.getWeibo_platform());
//					context.startActivity(intent);
				}else{
					intent.setClass(context, CheckCommentActivity.class);
					intent.putExtra("itemId", tempCommonEntity.getId());
					intent.putExtra("weiboID", tempCommonEntity.getWeibo_id());
					intent.putExtra("weiboPlatform", tempCommonEntity.getWeibo_platform());
					intent.putExtra("commentSum", tempCommonEntity.getComment_nums());
					context.startActivity(intent);
				}
			}
			break;
		case R.id.dispatch:
			if(tempCommonEntity.getWeibo_id().equals(AppConstant.TAG_AD)){
				return;
			}
			intent.setClass(context, TransferActivity.class);
			intent.putExtra("weiboImg",tempCommonEntity.getImg_url());
			intent.putExtra("weiboContent", tempCommonEntity.getContent());
			intent.putExtra("weibo_platform", tempCommonEntity.getWeibo_platform());
			intent.putExtra("weiboId", tempCommonEntity.getWeibo_id());
			context.startActivity(intent);//context,TransferActivity.class
			break;
		case R.id.weibo_img:
			Intent intentImg= new Intent(context,SaveImageActivity.class);
			intentImg.putExtra("imageUrl",tempCommonEntity.getImg_url());
			intentImg.putExtra("imageHeight",tempCommonEntity.getBig_height());
			intentImg.putExtra("imageWidth",tempCommonEntity.getBig_width());
			intentImg.putExtra("img_extension",tempCommonEntity.getImg_extension());
			context.startActivity(intentImg);
			break;
		case R.id.weibo_gifview:
			Intent intentGIF= new Intent(context,SaveImageActivity.class);
			intentGIF.putExtra("imageUrl",tempCommonEntity.getImg_url());
			intentGIF.putExtra("imageHeight",tempCommonEntity.getBig_height());
			intentGIF.putExtra("imageWidth",tempCommonEntity.getBig_width());
			intentGIF.putExtra("img_extension",tempCommonEntity.getImg_extension());
			context.startActivity(intentGIF);
			break;
		case R.id.layout_user_img:
			Intent intentHomepage= new Intent(context,HomePageActivity.class);
			intentHomepage.putExtra("weiboUrl",tempCommonEntity.getWeibo_url());
			context.startActivity(intentHomepage);
			break;
		}
		
	}
	
	public void abolish(String id){
		FavorateDAO favorateDAO = new FavorateDAO(context);
		favorateDAO.delete("id="+id, null);
		adapter.notifyDataSetChanged();
	}
}
